package com.woniu.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.project.client.ProductEntityCient;
import com.woniu.project.core.domain.*;
import com.woniu.project.core.domain.ProductSmallType;
import com.woniu.project.core.dto.ResultDto;
import com.woniu.project.core.exception.ProjectProductException;
import com.woniu.project.dao.ProductSmallTypeMapper;
import com.woniu.project.dto.productsmalltype.ProdcutSmallTypeSelectDto;
import com.woniu.project.params.productsmalltype.ProdcutSmallTypeAddParam;
import com.woniu.project.params.productsmalltype.ProdcutSmallTypeSelectParam;
import com.woniu.project.params.productsmalltype.ProdcutSmallTypeUpdateParam;
import com.woniu.project.service.IImageService;
import com.woniu.project.service.IProductService;
import com.woniu.project.service.IProductSmallTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2020-10-12
 */
@Service
@Transactional
public class ProductSmallTypeServiceImpl extends ServiceImpl<ProductSmallTypeMapper, ProductSmallType> implements IProductSmallTypeService {

    @Autowired
    private IImageService imageService;
    @Autowired
    private ProductEntityCient productEntityCient;
    @Autowired
    private IProductService productService;

    //根据是否有大类id和name进行不同的分页查询
    @Override
    public IPage<ProdcutSmallTypeSelectDto> select(ProdcutSmallTypeSelectParam prodcutSmallTypeSelectParam) {
        Page<ProductSmallType> page = new Page<>(prodcutSmallTypeSelectParam.getPage(), prodcutSmallTypeSelectParam.getSize());
        QueryWrapper<ProductSmallType> wrapper = new QueryWrapper<ProductSmallType>();
        wrapper.eq(prodcutSmallTypeSelectParam.getId() != null && !prodcutSmallTypeSelectParam.getId().equals(""),
                "product_big_type_id", prodcutSmallTypeSelectParam.getId());

        wrapper.like(prodcutSmallTypeSelectParam.getName() != null && !prodcutSmallTypeSelectParam.getName().equals(""),
                "product_small_type_name", prodcutSmallTypeSelectParam.getName());

        wrapper.orderByAsc("product_small_type_sort");
        IPage ipage = page(page, wrapper);
        //entity换为dto
        List<ProductSmallType> records = ipage.getRecords();
        List<ProdcutSmallTypeSelectDto> prodcutSmallTypeSelectDtos = new ArrayList<>();
        for (int i = 0; i < records.size(); i++) {
            ProdcutSmallTypeSelectDto prodcutSmallTypeSelectDto = new ProdcutSmallTypeSelectDto();
            BeanUtils.copyProperties(records.get(i), prodcutSmallTypeSelectDto);
            int imgId = records.get(i).getImageId();
            Image image = imageService.getById(imgId);
            prodcutSmallTypeSelectDto.setAddr(image.getImageAddr());
            prodcutSmallTypeSelectDtos.add(prodcutSmallTypeSelectDto);
        }
        ipage.setRecords(prodcutSmallTypeSelectDtos);
        return ipage;
    }

    //新增商品小类
    @Override
    public int addSmallType(ProdcutSmallTypeAddParam prodcutSmallTypeAddParam) {
        ProductSmallType productSmallType = new ProductSmallType();
        BeanUtils.copyProperties(prodcutSmallTypeAddParam, productSmallType);


        //图片增加
        Image image = new Image();
        String addr = prodcutSmallTypeAddParam.getAddr();
        image.setImageAddr(addr);
        imageService.save(image);

        //查询所有小类序号，如有序号等于当前序号则返回1
        QueryWrapper<ProductSmallType> wrapper = new QueryWrapper<ProductSmallType>();
        wrapper.eq("product_small_type_sort", productSmallType.getProductSmallTypeSort());
        List<ProductSmallType> list = list(wrapper);
        if (list.size() != 0) {
            return 1;
        }
        //小类增加
        productSmallType.setImageId(image.getImageId());
        save(productSmallType);
        return 0;
    }

    //修改商品小类
    @Override
    public int updateSmallType(ProdcutSmallTypeUpdateParam prodcutSmallTypeUpdateParam) {
        ProductSmallType productSmallType = new ProductSmallType();
        BeanUtils.copyProperties(prodcutSmallTypeUpdateParam, productSmallType);
        //小类图片修改
        Image image = new Image();
        ProductSmallType smallType = getById(prodcutSmallTypeUpdateParam.getProductSmallTypeId());
        image.setImageId(smallType.getImageId());
        String addr = prodcutSmallTypeUpdateParam.getAddr();
        image.setImageAddr(addr);
        imageService.updateById(image);
        productSmallType.setImageId(image.getImageId());

        //查询所有小类序号，如有序号等于当前序号则返回1
        QueryWrapper<ProductSmallType> wrapper = new QueryWrapper<ProductSmallType>();
        wrapper.eq("product_small_type_sort", productSmallType.getProductSmallTypeSort());
        wrapper.ne("product_small_type_id", prodcutSmallTypeUpdateParam.getProductSmallTypeId());
        List<ProductSmallType> list = list(wrapper);
        if (list.size() > 0) {
            return 1;
        }
        //小类修改
        updateById(productSmallType);
        return 0;
    }

    @Override
    public void deleteById(Integer id) {
        ResultDto resultDto = productEntityCient.selectBySmallTypeId(id);
        if (!resultDto.getData().toString().equals("[]")){
            throw new ProjectProductException(500, "该小类下还有商品，不能删除");
        }
        //删除图片件
        ProductSmallType smallType = getById(id);
        String imageAddr = imageService.getById(smallType.getImageId()).getImageAddr();
        //删除图片地址
        imageService.removeById(smallType.getImageId());
        //删除类型
        QueryWrapper<Product> wrapper = new QueryWrapper<Product>();
        wrapper.eq("product_small_type_id", id);
        productService.remove(wrapper);
        //删除小类
        removeById(id);
    }
}
